<?php
/*
	Copyright 2006, 2007, 2008, 2009, 2010 Bastiaan Grutters
    
    This file is part of Ages of Strife website.

    Ages of Strife website is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Ages of Strife website is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Ages of Strife website.  If not, see <http://www.gnu.org/licenses/>.
 */
include( '../global/initialize.php' );
$rulername = formatInput( $_POST[ 'rulername' ] );
$cityname = formatInput( $_POST[ 'cityname' ] );

$query = "SELECT count(*) FROM ruler WHERE user_id = " . $_SESSION[ 'user_id' ];
$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
$row = mysql_fetch_array($result, MYSQL_ASSOC);
if( $row[ 'count(*)' ] > 0 ) {
	header( "Location: ../overview/overview.php" );
	exit();
}
else {
	if( strlen( $rulername ) != strlen( $_POST[ 'rulername' ] ) ) {
		$message = translate( "Your ruler name contained invalid characters." );
	}
	elseif( strlen( $cityname ) != strlen( $_POST[ 'cityname' ] ) ) {
		$message = translate( "Your cityname contained invalid characters." );
	}
	elseif( strlen( $rulername ) < 3 ) {
		$message = translate( "Your ruler name is too short (minimum of 3 characters)." );
	}
	elseif( strlen( $cityname ) < 3 ) {
		$message = translate( "Your capital name is too short (minimum of 3 characters)." );
	}
	else {
		$query = "SELECT count(*) FROM ruler WHERE name = '" . $rulername . "'";
		$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
		$row = mysql_fetch_array($result, MYSQL_ASSOC);
		if( $row[ 'count(*)' ] > 0 ) {
			$_SESSION[ 'create_ruler_status' ] = $rulername . " " . translate( "is already in use, please choose a different ruler name." );
			header( "Location: sign_up.php?rulername=$rulername&capital=$cityname" );
			exit();
		}
		else {
			$query = "SELECT count(*) FROM village WHERE start_place = 1 AND ruler_id = -1";
			$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
			$row = mysql_fetch_array($result, MYSQL_ASSOC);
			if( $row[ 'count(*)' ] > 0 ) {
				$query = "SELECT count(*) FROM village WHERE name = '$cityname'";
				$result = mysql_query( $query ) or die( "Query failed : ". mysql_error() );
				$row = mysql_fetch_array( $result, MYSQL_ASSOC );
				if( $row[ 'count(*)' ] > 0 ) {
					$_SESSION[ 'create_ruler_status' ] = $cityname . " " . translate( "is already in use, please choose a different capital name." );
					header( "Location: sign_up.php?rulername=$rulername&capital=$cityname" );
					exit();
				}
				else {
					if( isset( $_SESSION[ 'user_just_registered' ] ) && $_SESSION[ 'user_just_registered' ] == 1 ) {
						unset( $_SESSION[ 'user_just_registered' ] );
					}
					
					$_SESSION[ 'user_just_signed_up' ] = 1;
					$game_turn = getCurrentTurn();
					$game_date = getCurrentDate();
					
					if( isset( $_POST[ 'letters' ] ) ) {
						$letters = 1;
					}
					else {
						$letters = 0;
					}
					
					if( isset( $_POST[ 'inactivity' ] ) ) {
						$inactivity_notification = 1;
					}
					else {
						$inactivity_notification = 0;
					}
					
					if( isset( $_POST[ 'civilian_news' ] ) ) {
						$civilian_news = 1;
					}
					else {
						$civilian_news = 0;
					}
					
					if( isset( $_POST[ 'operations_news' ] ) ) {
						$operations_news = 1;
					}
					else {
						$operations_news = 0;
					}
					
					if( isset( $_POST[ 'military_news' ] ) ) {
						$military_news = 1;
					}
					else {
						$military_news = 0;
					}
					
					if( isset( $_POST[ 'newsletter' ] ) ) {
						$news_letter = 1;
					}
					else {
						$news_letter = 0;
					}
					
					$query = "UPDATE users " .
							"SET letters = $letters, inactivity_notification = $inactivity_notification, civilian_news = $civilian_news, " .
								"operations_news = $operations_news, military_news = $military_news, news_letter = $news_letter " .
							"WHERE user_id = " . $_SESSION[ 'user_id' ];
					$result = mysql_query( $query ) or die( "Query failed : ". mysql_error() );

					$query = "SELECT season " .
							"FROM game " .
							"LIMIT 1";
					$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
					$row = mysql_fetch_array($result, MYSQL_ASSOC);
					$season = $row[ 'season' ];
					
					if( $season == 2 ) {
						$start_food = 45000;
					}
					elseif( $season == 3 || $season == 0 ) {
						$start_food = 180000;
					}
					else {
						$start_food = 1000;
					}
			
					$query = "INSERT INTO ruler ( name, user_id, gold, tax_level, start_turn, active_turn, food ) " .
							"VALUES ( '$rulername', '" . $_SESSION['user_id'] . "', '100', '3', '$game_turn', '$game_turn', $start_food )";
					$result = mysql_query( $query ) or die( "Query failed : ". mysql_error() );
					
					$query = "SELECT village_id FROM village WHERE start_place = 1 AND ruler_id = -1";
					$result = mysql_query( $query ) or die( "Query failed: " . mysql_error() );
					$row = mysql_fetch_array( $result, MYSQL_ASSOC );
					$village_id = $row[ 'village_id' ];
			
					$query = "SELECT ruler_id FROM ruler WHERE user_id = " . $_SESSION['user_id'];
					$result = mysql_query( $query ) or die( "Query failed: " . mysql_error() );
					$row = mysql_fetch_array( $result, MYSQL_ASSOC );
					$ruler_id = $row[ 'ruler_id' ];
					$_SESSION[ 'ruler_id' ] = $row[ 'ruler_id' ];
		
					$query = "UPDATE users SET ip = '" . $_SERVER[ 'REMOTE_ADDR' ] . "' WHERE user_id = " . $_SESSION[ 'user_id' ];
					$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
		
					$query = "UPDATE village " .
							"SET ruler_id = '$ruler_id', " .
								"name = '$cityname', " .
								"food_jobs_prefered = 4000, " .
								"max_population = 12000, " .
								"loyal_to = $ruler_id " .
							"WHERE village_id = $village_id";
					mysql_query( $query ) or die( "Query failed : ". mysql_error() );
			
					$query = "INSERT INTO scouted ( ruler_id, village_id ) VALUES ( $ruler_id, $village_id )";
					mysql_query( $query ) or die( "Query failed : ". mysql_error() );

					$query = "INSERT INTO ruler_task " .
							"( `ruler_id`, `task_id`, `updated` ) " .
							"VALUES ( $ruler_id, 1, 1 )";
					mysql_query( $query ) or die( "Query failed : ". mysql_error() );
					
					$subject = translate( "Welcome to" ) . " Ages of Strife";
					$message = translate( "This is a news item, these will keep you informed of events in your empire." ) . "\\r\\n" .
							"\\r\\n" .
							translate( "If this is your first time playing here are some pointers" ) . ":\\r\\n" .
							" - " . translate( "Complete the <a href=\\\"../overview/tasks.php\\\">tasks</a> to familiarize yourself with the game." ) . "\\r\\n" .
							" - " . translate( "You can find more information about the game in the <a href=\\\"../aosmanual/manual.php\\\">manual</a>." ) . "\\r\\n" .
							" - " . translate( "If you find a bug or have a game suggestion send it to <a href=\\\"../diplomacy/send_letter.php?to=Zworf\\\">Zworf</a>, the game administrator." ) . "\\r\\n" .
							"\\r\\n" .
							"\\r\\n" .
							translate( "Enjoy" ) . ".\\r\\n";
					
					$query = "INSERT INTO news ( ruler_id, type, news, date, subject, turn ) " .
							"VALUES ( $ruler_id, 2, '$message', '$game_date', '$subject', $game_turn )";
					mysql_query( $query ) or die( "Query failed : ". mysql_error() );
			
					mysql_close($link);
					header( "Location: ../overview/overview.php" );
					exit();
				}
			}
			else {
				$_SESSION[ 'create_ruler_status' ] = translate( "The current sector has just been filled, click sign up again to start in the new sector." );
				header( "Location: sign_up.php?rulername=$rulername&capital=$cityname" );
				exit();
			}
		}
	}
}
$_SESSION[ 'create_ruler_status' ] = $message;
header( "Location: sign_up.php?rulername=$rulername&capital=$cityname" );
?>
